package com.qys.livesMall.shop.controller.app;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.kafka.dao.shop.ShopQueryHandle;
import com.qys.livesMall.shop.constants.OrderStatus;
import com.qys.livesMall.shop.modal.entity.ShopOrderSku;
import com.qys.livesMall.shop.modal.entity.ShopSellTask;
import com.qys.livesMall.shop.modal.vo.ShopIndexDataVO;
import com.qys.livesMall.shop.modal.vo.ShopIndexOrderDataVO;
import com.qys.livesMall.shop.service.ShopGoodsSkuStocksService;
import com.qys.livesMall.shop.service.ShopOrderInfoService;
import com.qys.livesMall.shop.service.ShopOrderSkuService;
import com.qys.livesMall.shop.service.ShopSellTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * app-店铺页
 * @menu AppShopIndexController
 * @date 2023/08/15 15:20
 */
@RestController
@RequestMapping("/app/shopIndex/")
@Tag(name = "App-店铺数据", description = "App-店铺数据")
public class AppShopIndexController {

    @Autowired
    private ShopOrderInfoService shopOrderInfoService;

    @Autowired
    private ShopSellTaskService shopSellTaskService;

    @Autowired
    private ShopGoodsSkuStocksService shopGoodsSkuStocksService;

    @Autowired
    private ShopQueryHandle shopQueryHandle;
    @Autowired
    private ShopOrderSkuService shopOrderSkuService;

    /**
     *
     * 店铺统计

     * @return
     */
    @GetMapping("/data/{shopId}")
    @Operation(summary = "店铺统计")
    public R<ShopIndexDataVO> indexData(@PathVariable Long shopId){
        ShopIndexDataVO vo=new ShopIndexDataVO();
        //今日
        LocalDateTime start=LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime end=LocalDateTimeUtil.endOfDay(LocalDateTime.now());

        //昨日
        LocalDateTime yesStart=LocalDateTimeUtil.beginOfDay(LocalDateTime.now()).plusDays(-1);
        LocalDateTime yesEnd=LocalDateTimeUtil.endOfDay(LocalDateTime.now()).plusDays(-1);

        //订单
        vo.setYesterdayOrderNum(shopOrderInfoService.getOrderNum(shopId,yesStart,yesEnd));
        vo.setTodayOrderNum(shopOrderInfoService.getOrderNum(shopId,start,end));
        vo.setTotalOrderNum(shopOrderInfoService.getOrderNum(shopId,null,null));

        //带货
        LambdaQueryWrapper<ShopSellTask> taskQw=new LambdaQueryWrapper<>();
        taskQw.eq(ShopSellTask::getShopBaseId,shopId);
        taskQw.ge(ShopSellTask::getCreateTime,start);
        taskQw.lt(ShopSellTask::getCreateTime,end);
        Long todayNum=shopSellTaskService.count(taskQw);
        vo.setTodayTaskNum(todayNum==null?0:todayNum);
        LambdaQueryWrapper<ShopSellTask> yesTaskQw=new LambdaQueryWrapper<>();
        yesTaskQw.eq(ShopSellTask::getShopBaseId,shopId);
        yesTaskQw.ge(ShopSellTask::getCreateTime,yesStart);
        yesTaskQw.lt(ShopSellTask::getCreateTime,yesEnd);
        Long yesNum=shopSellTaskService.count(yesTaskQw);
        vo.setYesterdayTaskNum(yesNum==null?0:yesNum);
        LambdaQueryWrapper<ShopSellTask> totalTaskQw=new LambdaQueryWrapper<>();
        totalTaskQw.eq(ShopSellTask::getShopBaseId,shopId);
        Long totalNum=shopSellTaskService.count(totalTaskQw);
        vo.setTotalTaskNum(totalNum==null?0:totalNum);

        //销量
        vo.setYesterdayGoodsNum(shopOrderInfoService.getGoodsNum(shopId,yesStart,yesEnd));
        vo.setTodayGoodsNum(shopOrderInfoService.getGoodsNum(shopId,start,end));
        //vo.setTotalGoodsNum(shopOrderInfoService.getGoodsNum(shopId,null,null));

        vo.setTotalGoodsNum(shopGoodsSkuStocksService.totalStockNum(shopId));

        vo.setBrowseUserNum(shopQueryHandle.shopBrowseRecordCountByShopId(shopId));
        QueryWrapper<ShopOrderSku> qw=new QueryWrapper<>();
        qw.select("DISTINCT id").lambda()
                .eq(ShopOrderSku::getShopId,shopId)
                .ne(ShopOrderSku::getOrderStatus,OrderStatus.WAIT_PAY)
                .ne(ShopOrderSku::getOrderStatus,OrderStatus.CANCELED)
                .ne(ShopOrderSku::getOrderStatus, OrderStatus.RETURNED);
        ;
        vo.setOrderUserNum(shopOrderSkuService.count(qw));
        //
        //
        return R.ok(vo);
    }

    /**
     *
     * 店铺统计

     * @return
     */
    @GetMapping("/orderData/{shopId}")
    @Operation(summary = "店铺统计")
    public R<ShopIndexOrderDataVO> indexOrderData(@PathVariable Long shopId){

        ShopIndexOrderDataVO vo=new ShopIndexOrderDataVO();
        //今日
        LocalDateTime start=LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime end=LocalDateTimeUtil.endOfDay(LocalDateTime.now());
        //今日订单
        vo.setTodayOrderNum(shopOrderInfoService.getOrderNum(shopId,start,end));
        //待发货
        vo.setWaitShipNum(shopOrderInfoService.getWaitShipNum(shopId));
        //待售后
        vo.setWaitReturnNum(shopOrderInfoService.getWaitReturnNum(shopId));
        return R.ok(vo);
    }

}

